﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Core.Data;
using Forest.Cat.Domain.Entities;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Core;

namespace Forest.Cat.Domain.Services.JobHunting
{
    public partial class SavedJobService : ISavedJobService
    {
        #region Constants
        private const string SAVEDJOB_BY_ID_KEY = "savedjob.id-{0}";
        private const string SAVEDJOB_PATTERN_KEY = "savedjob.";
        #endregion

        #region Fields
        private readonly IRepository<SavedJob> _savedJobRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public SavedJobService(IRepository<SavedJob> savedJobRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _savedJobRepository = savedJobRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteSavedJob(SavedJob savedjob)
        {
            if (savedjob == null)
                throw new ArgumentNullException("SavedJob");

            _savedJobRepository.Delete(savedjob);
            _cacheManager.RemoveByPattern(SAVEDJOB_PATTERN_KEY);
            _eventPublisher.EntityDeleted(savedjob);
        }

        public SavedJob GetSavedJobById(Guid savedjobId)
        {
            if (savedjobId == Guid.Empty)
                return null;
            string key = string.Format(SAVEDJOB_BY_ID_KEY, savedjobId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _savedJobRepository.GetById(savedjobId);
                return cl;
            });
        }

        public virtual IPagedList<SavedJob> GetAllSavedJobs(int pageIndex, int pageSize)
        {
            var query = _savedJobRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<SavedJob>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertSavedJob(SavedJob savedjob)
        {
            if (savedjob == null)
                throw new ArgumentNullException("SavedJob");
            _savedJobRepository.Insert(savedjob);
            _cacheManager.RemoveByPattern(SAVEDJOB_PATTERN_KEY);
            _eventPublisher.EntityInserted(savedjob);
        }

        public virtual void UpdateSavedJob(SavedJob savedjob)
        {
            if (savedjob == null)
                throw new ArgumentNullException("SavedJob");
            _savedJobRepository.Update(savedjob);
            _cacheManager.RemoveByPattern(SAVEDJOB_PATTERN_KEY);
            _eventPublisher.EntityUpdated(savedjob);
        }
        #endregion

        #region Extend Methods
        /// <summary>
        /// Search PageList by JobseekerId
        /// </summary>
        /// <param name="jobseekerId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<SavedJob> GetSavedJobsByJobseekerId(Guid jobseekerId, int pageIndex, int pageSize)
        {
            var query = _savedJobRepository.Table.Where(item => item.JobseekerId == jobseekerId).OrderByDescending(o =>
                o.DateSaved);
            var items = new PagedList<SavedJob>(query, pageIndex, pageSize);
            return items;
        }

        public IQueryable<SavedJob> GetSavedJobsByJobseekerId(Guid jobseekerId)
        {
            return _savedJobRepository.Table.Where(item => item.JobseekerId == jobseekerId);
        }
        #endregion
    }
}
